const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { ADMIN, USER, SWIPER, MENU, MENUTYPE, RTICLEA, RTICLEATYPE, MOTION } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr } = require('../util.js');
/**
 * 所有请求方法
 */
/**
 *  登录
 */
function isAdminLogin(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${ADMIN} where name=? and password=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取用户的数目
 */
function getUserCount() {
    return new Promise((resolve, reject) => {
        query(`select count(userId) as counts from ${USER}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取分页用户数
 */
function getUserList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        Object.entries(like).forEach((item) => {
            if (item[1] != undefined && item[1] != "") {
                if (!String(condition).length) {
                    condition += " where "
                } else {
                    condition += " and "
                }
                condition += `${item[0]} like '%${item[1]}%'`
            }
        })
        query(`select  ${sqlCount} * from ${USER} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取管理员总数目
 */
function getAdminCount() {
    return new Promise((resolve, reject) => {
        query(`select count(id) as counts from ${ADMIN}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取管理员全部信息
 */
function GetAdminList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${ADMIN} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据管理员id查询管理员信息
 */
function GetAdminById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${ADMIN} where id=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id修改管理员信息
 */
function UpdateAdminById(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${ADMIN} SET ${sql} WHERE id=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id删除管理员
 */
function DeleteAdminById(id) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${ADMIN} WHERE id=?`, id)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加管理员
 */
function addAdmin(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${ADMIN}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取用户信息
 */
function GetUsersById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${USER} where userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除用户的方法
 */
function DeleteUserById(id) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${USER} WHERE userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加轮播图
 */
function uploadSwiperImg(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${SWIPER}(${obj.keys}) VALUES(${obj.vals})`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改轮播图
 */
function updataSwiperImg(swiperId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${SWIPER} SET ${sql} WHERE swiperId=${swiperId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有轮播图
 */
function getSwiperList() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${SWIPER} ORDER BY swiperIndex ASC`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除指定id的轮播图
 */
function deleteSwiperById(swiperId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${SWIPER} WHERE swiperId=?`, { swiperId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 更换轮播顺序
 */
function updataSwiperWz(data) {
    let sql = data.map(item => {
        return `UPDATE ${SWIPER} SET swiperIndex=${item.swiperIndex} WHERE swiperId=${item.swiperId}`
    })
    return new Promise((resolve, reject) => {
        query(sql)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 *根据swiperId查询轮播图的顺序
 */
function getSwiperIdByIndex(swiperId) {
    return new Promise((resolve, reject) => {
        query(`select *  from ${SWIPER} where swiperId=${swiperId}`)
            .then((res) => {
                resolve(res[0].swiperIndex)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取菜谱的总数
 */
function getMenuCount() {
    return new Promise((resolve, reject) => {
        query(`select count(menuId) as counts from ${MENU}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取菜谱信息列表
 */
function getMenuList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${MENU} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取菜谱信息
 */
function getMenuById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${MENU} where menuId=?`, { id })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id删除食谱
 */
function deleteMenuById(menuId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${MENU} WHERE menuId=?`, { menuId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有菜谱分类
 */
function getMenutypeAll() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${MENUTYPE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 新增菜谱分类
 */
function addMenutype(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT ignore INTO ${MENUTYPE}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除菜谱分类
 */
function delMenutype(menutypeId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${MENUTYPE} WHERE menutypeId=?`, { menutypeId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加菜谱
 */
function addMenu(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${MENU}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改菜谱
 */
function updataMenu(menuId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${MENU} SET ${sql} WHERE menuId=${menuId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有文章分类
 */
function getRticleatypeAll() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${RTICLEATYPE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 新增文章分类
 */
function addRticleatype(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT ignore INTO ${RTICLEATYPE}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除文章分类
 */
function delRticleatype(rticleatypeId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${RTICLEATYPE} WHERE rticleatypeId=?`, { rticleatypeId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取文章列表
 */
function getRticleaList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${RTICLEA} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加文章
 */
function addRticlea(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${RTICLEA}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取文章信息
 */
function getRticleaById(id) {
    return new Promise((resolve, reject) => {
        query(`select ${RTICLEA}.*,${ADMIN}.name as adminName from ${RTICLEA},${ADMIN} where rticleaId=? and ${ADMIN}.id=${RTICLEA}.adminId`, { id })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除文章
 */
function deleteRticleaById(rticleaId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${RTICLEA} WHERE rticleaId=?`, { rticleaId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改文章
 */
function updataRticlea(rticleaId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${RTICLEA} SET ${sql} WHERE rticleaId=${rticleaId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取菜谱的总数
 */
function getRticleatCount() {
    return new Promise((resolve, reject) => {
        query(`select count(rticleaId) as counts from ${RTICLEA}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取一周用户数据
 */
function getUserListWord() {
    return new Promise((resolve, reject) => {
        query(`select  ${sqlCount} * from ${USER} where YEARWEEK(date_format(userTime,'%Y-%m-%d'),1) = YEARWEEK(now(),1)`, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 获取文章分类数据
 */
function getRticleaTable(typeList) {
    let sql = []
    sql = typeList.map(item => {
        return `select count(rticleaId)  value,'${item.rticleatypeTitle}'  name from ${RTICLEA} where rticleaType='${item.rticleatypeTitle}'`
    })
    return new Promise((resolve, reject) => {
        query(sql)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取菜谱三餐分类信息
 */
function getMenuTimeTable(typeList) {
    let sql = []
    sql = typeList.map(item => {
        return `select count(menuId)  value,'${item.menuTime}'  name from ${MENU} where menuTime='${item.menuTime}'`
    })
    return new Promise((resolve, reject) => {
        query(sql)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}

/**
 * 获取运动项目总数目
 */
function getMotionCount() {
    return new Promise((resolve, reject) => {
        query(`select count(motionId) as counts from ${MOTION}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取运动项目全部数据
 */
function GetMotionList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${MOTION} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据运动项目id查询运动项目信息
 */
function GetMotionById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${MOTION} where motionId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id修改管理员信息
 */
function UpdateMotionById(motionId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${MOTION} SET ${sql} WHERE motionId=${motionId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id删除管理员
 */
function DeletemotionById(id) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${MOTION} WHERE motionId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加运动项目
 */
function addMotion(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${MOTION}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.isAdminLogin = isAdminLogin;
exports.getUserCount = getUserCount;
exports.getUserList = getUserList;
exports.GetUsersById = GetUsersById;
exports.DeleteUserById = DeleteUserById;
exports.getAdminCount = getAdminCount;
exports.GetAdminById = GetAdminById;
exports.UpdateAdminById = UpdateAdminById;
exports.DeleteAdminById = DeleteAdminById;
exports.addAdmin = addAdmin;
exports.GetAdminList = GetAdminList;
exports.uploadSwiperImg = uploadSwiperImg;
exports.updataSwiperImg = updataSwiperImg;
exports.getSwiperList = getSwiperList;
exports.deleteSwiperById = deleteSwiperById;
exports.getMenuCount = getMenuCount;
exports.getMenuList = getMenuList;
exports.getMenuById = getMenuById;
exports.getMenutypeAll = getMenutypeAll;
exports.addMenutype = addMenutype;
exports.delMenutype = delMenutype;
exports.addMenu = addMenu;
exports.updataSwiperWz = updataSwiperWz;
exports.getSwiperIdByIndex = getSwiperIdByIndex;
exports.deleteMenuById = deleteMenuById
exports.updataMenu = updataMenu
exports.getRticleatypeAll = getRticleatypeAll
exports.addRticleatype = addRticleatype
exports.delRticleatype = delRticleatype
exports.getRticleaList = getRticleaList
exports.addRticlea = addRticlea;
exports.getRticleaById = getRticleaById
exports.deleteRticleaById = deleteRticleaById;
exports.updataRticlea = updataRticlea;
exports.getRticleatCount = getRticleatCount;
exports.getUserListWord = getUserListWord;
exports.getRticleaTable = getRticleaTable;
exports.getMenuTimeTable = getMenuTimeTable;
exports.getMotionCount = getMotionCount;
exports.GetMotionList = GetMotionList;
exports.addMotion = addMotion;
exports.GetMotionById = GetMotionById;
exports.DeletemotionById = DeletemotionById;
exports.UpdateMotionById = UpdateMotionById;